#include "sys.h"
#include "net.h"
#include "log.h"
#include "ghttp.h"
#include "platform.h"


static int All_log_size = 0;

static int _prepare_log_file(){

	struct stat st;

	system("ps > "ALL_LOG_FILE);

	system("cat *.log >> "ALL_LOG_FILE);

	if(-1 == stat(ALL_LOG_FILE, &st)){
		log_syserr("stat "ALL_LOG_FILE" failed.");
		return -1;
	}

	All_log_size = st.st_size;
	// log_trace("All_log_size:%d", All_log_size);

	return 1;
}

static void transform_mac(char *parse_mac, char *mac){
	int i = 0, j = 0;

	for (i = 0; i < strlen(mac); ++i)
	{
		if(mac[i] != ':'){
			parse_mac[j++] = mac[i];
		}
	}
	// log_trace("parse_mac:%s", parse_mac);
}

void log_upload_report(char *url, char *probe_id, char *mac){
	FILE *fp = NULL;
	int fd = 0;
	char tmp_buf[128] = {0}, buffer[128] = {0}, parse_mac[32] = {0};
	char boundary_buf[64] = {0}, boundary_end_buf[64] = {0},  body_header[512] = {0};
	int body_header_offset = 0, boundary_end_len = 0;
    ghttp_request* req = NULL;

	if(!url){
		log_trace("url:%p, is NULL", url);
		return;
	}

	if(strlen(url) == 0){
		log_trace("url len is zero");
		return;
	}

	if( 0 > _prepare_log_file()){
		log_error("prepare log file failed.");
		return;
	}

	transform_mac(parse_mac, mac);

	req = ghttp_request_new();
	if(req == NULL){
		log_error("new ghttp request  failed.");
		goto out;
	}
	if (ghttp_set_uri(req, url) == -1) {
        log_error("http set uri error:%s", ghttp_get_error(req));
        goto out;
    }

    ghttp_set_type(req, ghttp_type_post);


    sprintf(boundary_buf,"Ghttp-boundary-%d", get_random());
    sprintf(tmp_buf, "multipart/form-data; boundary=%s", boundary_buf);
    boundary_end_len = sprintf(boundary_end_buf,"\r\n--%s--\r\n", boundary_buf);
    ghttp_set_header(req, "Content-Type", tmp_buf);
    ghttp_set_header(req, "Connection", "Keep-Alive");
	ghttp_set_sync(req, ghttp_async);

    body_header_offset += sprintf(body_header + body_header_offset, "--%s\r\n", boundary_buf);
    body_header_offset += sprintf(body_header + body_header_offset, "Content-Disposition: form-data; name=\"file\"; filename=\"%s_%s_%u.log\"\r\n", probe_id, parse_mac, time(NULL));
    body_header_offset += sprintf(body_header + body_header_offset, "Content-Type: application/octet-stream;chartset=UTF-8\r\n\r\n");
    ghttp_set_body(req, body_header, body_header_offset);
    // log_trace("body_header_offset:%d", body_header_offset);

    ghttp_prepare(req);

    sprintf(tmp_buf, "%d", All_log_size + body_header_offset + boundary_end_len); 
    ghttp_set_header(req, "Content-Length", tmp_buf);

    ghttp_process(req);

    fd = ghttp_get_socket(req);
    fp = fopen(ALL_LOG_FILE, "r");
    if(!fp){
    	log_syserr("can not open "ALL_LOG_FILE".");
    	goto out;
    }
    while(fgets(buffer, sizeof(buffer), fp) != NULL){

    	if( 0 > write(fd, buffer, strlen(buffer))){
    		log_syserr("write log error.");
    		break;
    	}
    	memset(buffer, 0, sizeof(buffer));
    }
    write(fd, boundary_end_buf, boundary_end_len);

out:
	if(req)
    	ghttp_request_destroy(req);
   	if(fp)
   		fclose(fp);
   	unlink(ALL_LOG_FILE);
}		